package com.example.smartcity_poverty.ui.AllService.VolunteerService.adapter

import android.content.Intent
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.VolunteerService.VtNewsModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.VolunteerService.activity.VtShowActivity
import kotlinx.android.synthetic.main.re_vt_home_list.*
import kotlinx.coroutines.Dispatchers
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

/**
 *  created by ikun
 *  ANDROID STUDIO - version 4.0
 *  email: jinkunwei41@gmail.com
 * --------------------------------
 *
 **/
class VtHomeNewsAdapter(
    val list: MutableList<VtNewsModel.RowsBean>,
    layoutId: Int
) : BaseRecyclerAdapter<VtNewsModel.RowsBean>(list, layoutId) {
    override fun setData(
        view: BaseViewHolder.ViewFind,
        data: VtNewsModel.RowsBean,
        position: Int,
        holder: BaseViewHolder
    ) {
        val d = list[position]
        val fv = holder.viewFind
        val con = holder.viewFind.view?.context

        fv.vtNewsTitle.text = d.title
        fv.vtNewsClass.text = "承办单位: ${d.undertaker}"
        fv.vtNewsTime.text = "活动开始时间: ${d.startAt}"
        fv.vtNewsUser.text = "人员要求: ${d.requireText}"

        fv.vtNewsOk.setOnClickListener {
            coroutine(Dispatchers.Main) {
                api.getVtTrue(getToken(), JSONObject().apply {
                    put("activityId", d.id.toString())
                    put("newState", "true")
                }.toString().toRequestBody("application/json".toMediaTypeOrNull())).apply {
                    this.msg.show()
                }
            }

        }

        holder.itemView.setOnClickListener {
            con?.startActivity(
                Intent(con, VtShowActivity::class.java).putExtra(
                    "id",
                    d.id.toString()
                )
            )
        }

    }
}